package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import sjtu.ist.warehouse4ws.model.CustEpcunits;
import sjtu.ist.warehouse4ws.model.Epcunits;
import sjtu.ist.warehouse4ws.service.EpcunitsService;
import sjtu.ist.warehouse4ws.service.cust.CustEpcunitsService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ShowEpcListAction extends ActionSupport implements ApplicationContextAware {
	private EpcunitsService epcUnitsService;
	private List<Epcunits> epcUnitsList = new ArrayList<Epcunits>();
    private CustEpcunitsService custEpcunitsService;
    
    private ApplicationContext applicationContext = null;
	private String gid;
	private String vid;
	
	public void setCustEpcunitsService(CustEpcunitsService custEpcunitsService) {
		this.custEpcunitsService = custEpcunitsService;
	}

	//获得EpcUnits列表
	public String showEpcList(){
		
		Subject currentUser = SecurityUtils.getSubject();		
		Integer company=(Integer) currentUser.getSession().getAttribute("companyId");
		
		if (!currentUser.hasRole("superadmin")){
			this.epcUnitsList=new ArrayList<Epcunits>();
			List<CustEpcunits> list=custEpcunitsService.getCustEpcunitsList();
			for(CustEpcunits ce : list){
				epcUnitsList.add(ce);
			}
		
		}else {

			
			//epcUnitsList=epcUnitsService.getEpcunitsList();
			epcUnitsList=epcUnitsService.getEpcunitsListByGroupAndVersion(gid,vid,company);

//			List<Epcunits> templist=epcUnitsService.getEpcunitsList();
//		    this.epcUnitsList = new ArrayList<CustEpcunits>();
//		
//	
//		    for(Epcunits e : templist){
//			    if(privilegeService.checkPrivilege(e.getFileId(),userCompany,userType, "read")){
//				     epcUnitsList.add((CustEpcunits) e);
//			    }
//		     }
		}
		return SUCCESS;
	}
	
	@JSON(name = "totalEpcUnits")

	public int getTotalEpcUnits() {
		return this.epcUnitsList.size();

	}

	@JSON(name = "EpcUnits")
	public List<Epcunits> getEpcUnitsList() {
		return this.epcUnitsList;
	}
	
	// 注入EpcUnitsService
	public void setEpcunitsService(EpcunitsService epcunitsService) {
		this.epcUnitsService = epcunitsService;
	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext = applicationContext;
	}

	public String getGid() {
		return gid;
	}

	public void setGid(String gid) {
		this.gid = gid;
	}

	public String getVid() {
		return vid;
	}

	public void setVid(String vid) {
		this.vid = vid;
	}

}
